/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.model;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

/**
 *
 * @author Lunayo
 */
public class MultipleFillInBlankQuestion extends Question {
    private String[] studentAnswer;
    private List<String[]> correctAnswer;
    private boolean isCorrect;
    private boolean[] blankAssesment;
    private String questionDocument;
    
    public MultipleFillInBlankQuestion() {
        super();
        correctAnswer = new ArrayList<String[]>();
    }    
    
    public void setBlankAssesment(boolean[] blankAssesment) {
        this.blankAssesment = blankAssesment;
    }
    
    public boolean[] getBlankAssesment() {
        return blankAssesment;
    }

    public String[] getStudentAnswer() {
        return this.studentAnswer;
    }

    public void setStudentAnswer(String[] studentAnswer) {
        this.studentAnswer = studentAnswer;
    }
    
    public List<String[]> getCorrectAnswer() {
        return this.correctAnswer;
    }

    public void setCorrectAnswer(List<String[]> correctAnswer) {
        this.correctAnswer = correctAnswer;
    }
    
    public void setIsCorrect(boolean isCorrect) {
        this.isCorrect = isCorrect;
    }
    
    public int getBlankCount() {
        Pattern pattern = Pattern.compile("\\([0-9]\\)");
        Matcher  matcher = pattern.matcher(getQuestionText());
        int numberOfBlank = 0;
        while (matcher.find()) {
            numberOfBlank++;
        }
        return numberOfBlank;
    }
    
    public void setQuestionDocument(HTMLDocument questionDocument) {
        try {
            StringWriter writer = new StringWriter();
            HTMLEditorKit kit = new HTMLEditorKit(); 
            kit.write(writer, questionDocument, 0, questionDocument.getLength());
            String s = writer.toString();
            this.questionDocument = s;
        } catch (IOException ex) {
            Logger.getLogger(MultipleFillInBlankQuestion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadLocationException ex) {
            Logger.getLogger(MultipleFillInBlankQuestion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public HTMLDocument getQuestionDocument() {
        Reader stringReader = new StringReader(questionDocument);
        HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        try {
            htmlKit.read(stringReader, htmlDoc, 0);
        } catch (IOException ex) {
            Logger.getLogger(MultipleFillInBlankQuestion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadLocationException ex) {
            Logger.getLogger(MultipleFillInBlankQuestion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return htmlDoc;
    }
    
    public boolean isCorrect() {
        return isCorrect;
    }
}
